// GradeServiceImpl.java 服务层实现
package com.example.studentmanagement2.service.Impl;

import com.example.studentmanagement2.dto.GradeDetail;
import com.example.studentmanagement2.model.Grade;
import com.example.studentmanagement2.repository.Repository.GradeRepository;
import com.example.studentmanagement2.service.GradeService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GradeServiceImpl implements GradeService {
    private final GradeRepository gradeRepository;

    public GradeServiceImpl(GradeRepository gradeRepository) {
        this.gradeRepository = gradeRepository;
    }

    @Override
    public Grade recordGrade(Grade grade) {
        if (gradeRepository.existsByStudentAndCourse(grade.getStudentId(), grade.getCourseId())) {
            gradeRepository.update(grade);
        } else {
            gradeRepository.save(grade);
        }
        return grade;
    }
    @Override
    public List<GradeDetail> getGradeDetails(String studentId) {
        return gradeRepository.findGradeDetails(studentId);
    }

    @Override
    public List<GradeDetail> getGradesByCourse(String courseId) {
        return gradeRepository.findByCourseWithDetails(courseId);
    }



}
